package com.sgb.kjwl.viewmodel.demand;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.DemandHallListEntity;
import com.sgb.kjwl.model.entity.EnquiryTypeEntity;
import com.sgb.kjwl.model.entity.HomeMenuEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.demandHall.DemandHallListFragment2;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.entity.MenuEntity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DemandHallTabModel2 extends BaseViewModel {
    private DemandHallListFragment2 fragment;
    private HomeRepertory repertory;
    private final int pageLimit = 10;

    public DemandHallTabModel2(DemandHallListFragment2 fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //    获取状态类型
    public void getEnquiryType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getEnquiryType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.getEnquirtTypeList(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    //    获取用户类型
    public void getUserType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getUserType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.getUserTypeList(response.body().getData());

                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    //    获取排序类型
    public void getSortType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getSortType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.getSortTypeList(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    //获取数据列表信息
    public void getDemandHallList(String userid, String sortid, String enquiryid, String fcatName, String searchname, int page) {
        Map map = new HashMap();
        if (!TextUtils.isEmpty(userid)) {
            map.put("authMethod", userid);
        }
        map.put("limit", pageLimit);
        map.put("page", page);
        map.put("sortType", sortid);
        if (!TextUtils.isEmpty(enquiryid)) {
            map.put("status", enquiryid);
        }
        map.put("fcatName", fcatName + "");
        map.put("tcatName", searchname + "");
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<ListResponse<List<DemandHallListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Response<BaseEntity<ListResponse<List<DemandHallListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.setDemandHallList(response.body().getData().getList());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    //需求列表详情界面
    public void showUrl(String no,String shareTitle) {
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("enquiryDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!TextUtils.isEmpty(response.body().getData().getUrl())) {
                        String url = response.body().getData().getUrl() + "?no=" + no;
                        WebViewX5HostActivity.start(fragment.getActivity(), url,"","customerinquiry",shareTitle);

                        //存储商品跳转h5标记
                        SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "event_shopping_list");
                    } else {
                        WebViewX5HostActivity.start(fragment.getActivity(), "");
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
            }
        });
    }




    //获取跳转页需要传的数据
    public void recommendMenu(int position, boolean isMap) {
        APIManager.getInstance().getMaterialAPI().recommendMenuNew().enqueue(new ICallBack<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity>> data) {
                fragment.showImageButtonData(data.getData(), position, isMap);
            }
        });
    }

    public void recomandCate(boolean isRefresh) {
        Type type = new TypeToken<List<HomeMenuEntity>>() {
        }.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.HOME_MEAU, type);
        List<HomeMenuEntity> data = new ArrayList<>();
        if (null != obj && isRefresh == false) {
            List<HomeMenuEntity> dataList = (List<HomeMenuEntity>) obj;
            data.addAll(dataList);
            fragment.showRecommendCate(data, isRefresh);
        }
        if (data.size() == 0 || isRefresh) {
            getTuijianData(isRefresh);
        }
    }


    /**
     * 获取大首页推荐数据
     *
     * @param isRefresh
     */
    private void getTuijianData(boolean isRefresh) {
        APIManager.getInstance().getBigHomeAPI().getAllRecomandCate().enqueue(new ICallBack<BaseEntity<List<HomeMenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<HomeMenuEntity>> data) {
                if (data == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    //获取从网络请求下来的数据的哈希值
                    String mHashNet = DatalUtils.getInstance().strToHashKey(data.getData());
                    if (data.getData() != null && data.getData().size() > 0) {
                        String cacheOperationHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.HOME_MEAU);
                        if (!mHashNet.equals(cacheOperationHash)) {
                            fragment.showRecommendCate(data.getData(), false);
                            DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.HOME_MEAU, data.getData());
                        } else {
                            fragment.showRecommendCate(data.getData(), isRefresh);
                        }
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

}
